<template>
	<view class="content">
		<view class="head">
			<u-navbar leftIcon="arrow-leftward" rightText='篩選' @rightClick="filter" :placeholder='true'
				@leftClick="leftClick" title="報價車訂單">
			</u-navbar>
		</view>
		<!-- 	<view class="search">
			<u-search :show-action="false" borderColor="#7A7DEE" bgColor="#FFFFFF" placeholder="請輸入訂單號查詢"
				v-model="keyword"></u-search>
			<view class="title-box u-flex" @click="filter">
				<text class="filter-title">日期筛选</text>
			</view>
		</view> -->
		<view class="nav">
			<view :class="['nav-item', { active: currentIndex === index }]" @click="changeTab(index)"
				v-for="(item,index) in tabs" :key="index">
				{{item}}
			</view>
		</view>
		<view class="dindan" v-for="(item,index) in quotelist" :key="index">
			<view class="dindan-no">
				<view class="no-l">
					{{item.quote_sn}}
				</view>
				<view class="no-r">
					<view v-if="item.complete_status==0||item.complete_status==10" class="">
						{{item.quote_status==0? "商戶未確認": "商戶已報價"}}

					</view>
					<view v-else class="">
						{{item.complete_status==5? "未審批": "未通過"}}
					</view>
				</view>
			</view>
			<view class="">
				<view class="goods-detail" v-for="(item,index) in item.goods" :key="index" @click="todetails(item)">
					<view class="detail-left">
						<view class="goods-left">
							<view class="goods-img">
								<image :src="getFirstImageLink(item.goods_images)" mode="aspectFit"></image>
							</view>
						</view>
						<view class="size">
							<view class="goods-title">{{item.goods_name}}</view>
							<view class="center">
								<text> {{item.attr_value}}</text>
								<text>x{{item.goods_number}}</text>
							</view>
							<view class="bottom">
								<view class="goods-price">MOP {{item.unit_price}}</view>
								<view class="detail-right">
								</view>
							</view>
						</view>
					</view>
				</view>
				<view class="jiage">
					共 <view style="color:#645AED ;">{{getTotalGoodsNumber(item.goods)}}</view>件，折後價格：<view
						style="color:#645AED ;">MOP
						{{getTotalPrice(item.goods)}}
					</view>
				</view>
				<view class="threebtn">
					<view class="btn green" @click="addcart(item)">
						加入購物車
					</view>
				</view>
			</view>
		</view>
		<u-popup :show="show" mode="right" @close="closeWindows" @open="open">
			<view class="dialog">
				<view>
					<view class="title">日期</view>
					<!-- 日期选择器 -->
					<view class="cbox">
						<view class="cbox1">
							<view class="cbox-item" @click="toStart">
								<view class="time-item">
									{{beginTime || '开始日期'}}
								</view>
							</view>
							<view class="lines" style="color:#BEBEBE ;">-</view>
							<view class="cbox-item" @click="toEnd">
								<view class="time-item">
									{{endTime ||'结束日期'}}
								</view>
							</view>
						</view>
					</view>
				</view>
				<view class="btn">
					<view class="btn1" @click="reset">
						重置
					</view>
					<view class="btn2" @click="submit">
						确定
					</view>
				</view>
			</view>
		</u-popup>
		<u-datetime-picker ref="datetimePicker" :show="showTime" v-model="valueStart" mode="date" :formatter="formatter"
			title="开始时间" @confirm="confirmStart" @cancel="cancelStart"></u-datetime-picker>
		<u-datetime-picker ref="datetimePicker" :show="showTime1" v-model="valueEnd" mode="date" :formatter="formatter"
			title="结束时间" @confirm="confirmEnd" @cancel="cancelEnd"></u-datetime-picker>

	</view>
</template>

<script>
	import {
		getquoteorder,
		addquotecart
	} from '@/network/api/user.js'
	export default {
		data() {
			return {
				valueStart: Number(new Date()),
				valueEnd: Number(new Date()),
				beginTime: '',
				endTime: '',
				showTime: false,
				showTime1: false,
				quotelist: [],
				choiceIndex: 0, //选择位置
				show: false,
				tabs: ['全部', '已審批', '未審批', '未通過'],
				currentIndex: 0,
				keyword: "",
				data: {
					lang: 'cn',
					release: '',
					relend: '',
					complete_status: '',
					sortAttr: '',
					sortMode: '',
					page: 1
				},
				page: 1
			}
		},
		components: {},
		onShow() {
			this.page = 1
      this.data.page=1
			this.quotelist = []
			this.getquoteorderlist()
		},
		onReachBottom() {
			console.log('我到底了');
			// if (this.page < this.last_page) {
			// this.page += 1
			// this.getMycollect()
			// }

			this.data.page++
			this.getquoteorderlist()
		},
		methods: {
			async addcart(item) {
				console.log(item);
				const from = item.goods.map(item => {
					return {
						rec_id: item.rec_id,
						quote_id: item.order_id,
						goods_number: item.goods_number,
						goods_id: item.goods_id,
						goods_images: item.goods_images,
						stock: item.stock,
						goods_attr_id: item.goods_attr_id,
						numbering: item.numbering,
						goods_sn: item.goods_sn,
						specification_numbering: item.specification_numbering
					}
				})
				let data = {
					lang: 'cn',
					goods: from,
					quote_sn: item.quote_sn
				}
				const res = await addquotecart(data)
				if (res.data.code == 1) {
					uni.showToast({
						title: '加入購物車成功',
						icon: 'success',
						duration: 2000
					})
				}
			},
			getTotalPrice(item) {
				// 计算总价格
				return item.reduce((total, goods) => total + goods.goods_price * goods.goods_number, 0);
			},
			getTotalGoodsNumber(item) {
				// 计算商品总数量
				return item.reduce((total, goods) => total + goods.goods_number, 0);
			},
			async getquoteorderlist() {
				// 转圈
				uni.showLoading({
					title: '加载中'
				})
				const res = await getquoteorder(this.data)
				uni.hideLoading()
				let newquotelist = res.data.quote
				if (newquotelist.length == 0) {
					uni.showToast({
						title: '暫無數據',
						icon: 'none',
						duration: 2000
					})
				}
				this.quotelist = [...this.quotelist, ...newquotelist]
			},
			todetails(item) {
				uni.navigateTo({
					url: `/pages/mine/quotationCar/details?quote_id=${item.quote_id}`
				})
			},
			closeWindows() {
				this.show = false
			},
			formatter(type, value) {
				if (type === 'year') {
					return `${value}年`
				}
				if (type === 'month') {
					return `${value}月`
				}
				if (type === 'day') {
					return `${value}日`
				}
				return value
			},
			toStart() {
				this.showTime = true
				this.showTime1 = false
			},
			toEnd() {
				this.showTime1 = true
				this.showTime = false
			},
			confirmStart(e) {
				let date = new Date(e.value);
				let y = date.getFullYear();
				let MM = date.getMonth() + 1;
				MM = MM < 10 ? ('0' + MM) : MM; //月补0
				let d = date.getDate();
				d = d < 10 ? ('0' + d) : d; //天补0
				this.beginTime = y + '-' + MM + '-' + d; //年月日
				this.data.release = this.beginTime
				this.showTime = false
			},
			cancelStart(e) {
				this.showTime = false
			},
			confirmEnd(e) {
				let date = new Date(e.value);
				let y = date.getFullYear();
				let MM = date.getMonth() + 1;
				MM = MM < 10 ? ('0' + MM) : MM; //月补0
				let d = date.getDate();
				d = d < 10 ? ('0' + d) : d; //天补0
				this.endTime = y + '-' + MM + '-' + d; //年月日
				this.data.relend = this.endTime
				this.showTime1 = false
			},
			cancelEnd(e) {
				this.showTime1 = false
			},
			open() {

			},
			reset() {
				this.data.release = '',
					this.data.relend = '',
					this.endTime = '',
					this.beginTime = '',
					this.quotelist = []
				this.data.page = 1
				this.getquoteorderlist()
				this.show = false
			},
			submit() {
				this.quotelist = []
				this.getquoteorderlist()
				this.show = false
			},
			filter() {
				this.show = true
			},
			changeTab(index) {
				this.currentIndex = index;
				if (this.currentIndex == 0) {
					this.data.complete_status = ''
				}
				if (this.currentIndex == 1) {
					this.data.complete_status = 10
				}
				if (this.currentIndex == 2) {
					this.data.complete_status = 5
				}
				if (this.currentIndex == 3) {
					this.data.complete_status = 15
				}
				this.quotelist = []
				this.data.page = 1
				this.getquoteorderlist()

			},
			leftClick() {
				uni.switchTab({
					url: '/pages/mine/index'
				})
			}
		}
	}
</script>

<style lang="scss">
	@import "@/static/iconfont/iconfont.css";

	page {
		background: #f5f6f9;
	}

	.bg {
		background-color: white;
	}

	.content {
		.search {
			background-color: white;
			display: flex;
			align-items: center;
			padding: 0 32rpx;

			.title-box {
				display: flex;
				margin-left: 22rpx;
				align-items: center;
			}
		}

		.nav {
			background-color: white;
			height: 88rpx;
			line-height: 88rpx;
			display: flex;
			justify-content: space-around;
			font-size: 28rpx;
			font-family: PingFang SC;
			font-weight: 500;
			color: #666666;
		}

		.active {
			font-size: 28rpx;
			font-family: PingFang SC;
			font-weight: bold;
			color: #1F1F1F;
			border-bottom: 5rpx solid #645AED;
		}

		.jiage {
			display: flex;
			justify-content: flex-end;
			align-items: center;
			margin-top: 22rpx;
			font-size: 28rpx;
			font-family: PingFang SC;
			font-weight: bold;
		}

		.threebtn {
			margin-top: 40rpx;
			display: flex;
			justify-content: flex-end;
			font-size: 28rpx;
			font-family: PingFang SC;
			font-weight: 500;
			color: #333333;

			.btn {
				margin-left: 20rpx;
				text-align: center;
				line-height: 60rpx;
				width: 170rpx;
				height: 60rpx;
				border: 1rpx solid #CBCBCB;
				border-radius: 20rpx;
			}

			.green {

				border: 1rpx solid #645AED;
				color: #645AED;
			}
		}



		.dindan {
			margin-top: 16rpx;
			background: white;
			padding: 32rpx;

			.dindan-no {
				height: 84rpx;
				border-bottom: 1rpx solid #F1F1F1;
				display: flex;
				justify-content: space-between;
				font-size: 28rpx;
				font-family: PingFang SC;
				font-weight: 500;

				.no-l {
					color: #3A3A3A;
				}

				.no-r {
					color: #645AED
				}
			}

			.goods {
				line-height: 80rpx;
				background-color: #FFFFFF;

				&-detail {
					display: flex;
					padding: 16rpx 0;
					// margin: 0 32rpx;
					background-color: #fff;
					justify-content: space-between;
					align-items: center;

					.detail-left {
						flex: 1;
						display: flex;

						.goods-left {
							display: flex;
							align-items: center;

							.goods-img {
								width: 180rpx;
								height: 180rpx;
								background: #FFFFFF;
								border: 1rpx solid #DEDEDE;
								border-radius: 30rpx;
								display: flex;
								justify-content: center;
								align-items: center;

								image {
									max-width: 90%;
									max-height: 90%;
								}
							}
						}
					}

					.size {
						flex: 1;
						display: flex;
						justify-content: space-between;
						flex-direction: column;
						margin-left: 30rpx;

						.center {
							justify-content: space-between;
							display: flex;
							font-size: 22rpx;
							font-family: PingFang SC;
							font-weight: 500;
							color: #999999;
							margin-top: 8rpx;
						}

						.bottom {
							margin-top: 22rpx;
							display: flex;
							align-items: center;
							justify-content: space-between
						}

						.goods-title {
							font-size: 28rpx;
							font-family: PingFang SC;
							font-weight: 500;
							color: #333333;
						}

						.goods-price {
							font-size: 28rpx;
							font-family: PingFang SC;
							font-weight: bold;
							color: #4949C3;
						}
					}

					.detail-right {
						.stepper {
							display: flex;
							// align-items: center;
							vertical-align: middle;

							.stepper-value {
								margin: 0 20rpx;
								font-size: 24rpx;
								line-height: 43rpx;
								font-family: PingFang SC;
								font-weight: 500;
								color: #333333;
							}

							.stepper-btn {
								image {
									width: 44rpx;
									height: 44rpx;
								}
							}
						}

						text {
							width: 50rpx;
							line-height: 50rpx;
							text-align: center;
							display: inline-block;
							background-color: #F7F7F7;
							margin-right: 10rpx;
						}

						.add {
							color: #FA4305;
							border-radius: 10rpx 30rpx 30rpx 10rpx;
							margin-right: 20rpx;
						}

						.subtract {
							border-radius: 30rpx 10rpx 10rpx 30rpx;
						}
					}
				}
			}

		}

		.main {
			margin-top: 16rpx;
			padding: 0 32rpx;

			.order-item {
				padding: 32rpx 0;
				font-size: 28rpx;
				font-family: PingFang SC;
				font-weight: 500;
				color: #999999;
				border-bottom: 1rpx solid #DEDEDE;
				;
			}

			.order-item:last-child {
				border-bottom: none;
			}
		}

		.dialog {
			// padding: 0 31rpx;
			width: 640rpx;

			.title {
				margin: 0 31rpx;
				margin-top: 80rpx;
				margin-bottom: 31rpx;
				font-size: 26rpx;
				font-family: PingFang SC;
				font-weight: bold;
				color: #000000;
			}

			.cbox {
				margin: 0 31rpx;
				display: flex;
				justify-content: space-between;
				flex-wrap: wrap;

				.citem {
					margin-bottom: 32rpx;
					text-align: center;
					align-items: center;
					width: 185rpx;
					height: 54rpx;
					line-height: 54rpx;
					background: #F5F6F9;
					border-radius: 16rpx;
					font-size: 26rpx;
					font-family: PingFang SC;
					font-weight: 500;
					color: #000000;
				}

				.active {
					border: 1px solid #645AED;
					background: #E5E7FA;
					color: #645AED;
				}
			}

			.cbox1 {
				margin: 0 32rpx;
				display: flex;
				align-items: center;
				justify-content: space-between;

				.lines {
					padding: 10rpx;
				}

				.cbox-item {
					.time-item {
						width: 220rpx;
						background-color: #f2f2f2;
						height: 64rpx;
						border-radius: 32rpx;
						text-align: center;
						line-height: 64rpx;
						font-size: 28rpx;
						font-family: PingFang SC;
						font-weight: 500;
						color: #999999;
					}
				}
			}

			.btn {
				position: absolute;
				left: 31rpx;
				right: 31rpx;
				bottom: 15rpx;
				display: flex;
				justify-content: space-between;

				.btn1 {
					width: 280rpx;
					height: 68rpx;
					text-align: center;
					line-height: 68rpx;
					border: 1px solid #CBCBCB;
					opacity: 1;
					border-radius: 24rpx;
					font-size: 28rpx;
					font-family: PingFang SC;
					font-weight: 500;
					color: #FFFFFF;
					font-size: 28rpx;
					font-family: PingFang SC;
					font-weight: 500;
					color: #3A3A3A;
				}

				.btn2 {
					width: 280rpx;
					height: 68rpx;
					text-align: center;
					background: #645AED;
					line-height: 68rpx;
					border: 1px solid #CBCBCB;
					opacity: 1;
					border-radius: 24rpx;
					font-size: 28rpx;
					font-family: PingFang SC;
					font-weight: 500;
					color: #FFFFFF;
				}
			}
		}

	}
</style>
